package com.txunda.zhuohired.aty.user.resume;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.tools.RegexUtils;
import com.ants.theantsgo.util.L;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.smarttop.library.bean.City;
import com.smarttop.library.bean.County;
import com.smarttop.library.bean.Province;
import com.smarttop.library.bean.Street;
import com.smarttop.library.widget.AddressSelector;
import com.smarttop.library.widget.BottomDialog;
import com.smarttop.library.widget.OnAddressSelectedListener;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.user.resume.PersonalBean;
import com.txunda.zhuohired.dialog.JoinJobDialog;
import com.txunda.zhuohired.dialog.SelectBirthdayDialog;
import com.txunda.zhuohired.dialog.SexDialog;
import com.txunda.zhuohired.http.pst.MemberPst;
import com.txunda.zhuohired.http.pst.ResumePst;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/6 17:26
 * 功能描述：
 * 联系方式：jiahuanchen@126.com
 */

public class PersonalAty extends BaseAty implements AddressSelector.onSelectorAreaPositionListener,
        OnAddressSelectedListener, AddressSelector.OnDialogCloseListener {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * 姓名
     */
    @ViewInject(R.id.et_personal_name)
    private EditText et_personal_name;
    /**
     * 保存
     */
    @ViewInject(R.id.tv_layout_white_right)
    private TextView tv_layout_white_right;
    /**
     * 性别
     */
    @ViewInject(R.id.tv_personal_sex)
    private TextView tv_personal_sex;
    /**
     * 出生年月
     */
    @ViewInject(R.id.tv_personal_birthday)
    private TextView tv_personal_birthday;
    /**
     * 参加工作时间
     */
    @ViewInject(R.id.tv_personal_join_job_time)
    private TextView tv_personal_join_job_time;
    /**
     * 现居地
     */
    @ViewInject(R.id.tv_personal_city)
    private TextView tv_personal_city;
    /**
     * 手机号
     */
    @ViewInject(R.id.et_personal_phone)
    private EditText et_personal_phone;
    /**
     * 电子邮箱
     */
    @ViewInject(R.id.et_personal_mail)
    private EditText et_personal_mail;


    private BottomDialog dialog;
    private int provincePosition;
    private int cityPosition;
    private int countyPosition;
    private int streetPosition;
    private String provinceCode = "";
    private String cityCode = "";
    private String countyCode = "";
    private String streetCode = "";

    private String token = "";
    private ResumePst resumePst;
    private String name = "";//名称
    private String phone = "";//电话
    private String mail = "";//邮箱
    private String sex = "";//性别
    private String birthday = "";//出生年月
    private String address = "";//现居城市
    private String job_time = "";//参加工作时间
    private MemberPst memberPst;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("个人信息");
        tv_layout_white_right.setVisibility(View.VISIBLE);
        tv_layout_white_right.setText("保存");
    }

    @Override
    @OnClick({R.id.ll_personal_sex, R.id.ll_personal_birthday, R.id.ll_personal_join_job_time, R.id.ll_personal_city,
            R.id.tv_layout_white_right})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_personal_sex://选择性别
                SexDialog sexDialog = new SexDialog(this, new SexDialog.SexListener() {
                    @Override
                    public void onSex(String type) {
                        tv_personal_sex.setText(type);
                    }
                });
                sexDialog.show();
                break;
            case R.id.ll_personal_birthday://出生年月
                SelectBirthdayDialog selectBirthdayDialog = new SelectBirthdayDialog(this, new SelectBirthdayDialog
                        .SelectBirthdayListener() {


                    @Override
                    public void onSelectBirthday(String birthday) {
                        tv_personal_birthday.setText(birthday);
                    }
                });
                selectBirthdayDialog.show();
                break;
            case R.id.ll_personal_join_job_time://参加工作时间
                JoinJobDialog joinJobDialog = new JoinJobDialog(this, "2", new JoinJobDialog.JoinJobListener() {
                    @Override
                    public void onJoinJob(String birthday) {
                        tv_personal_join_job_time.setText(birthday);
                    }
                });
                joinJobDialog.show();
                break;
            case R.id.ll_personal_city://现居地
                if (dialog != null) {
                    dialog.show();
                } else {
                    dialog = new BottomDialog(this);
                    dialog.setOnAddressSelectedListener(this);
                    dialog.setDialogDismisListener(this);
                    dialog.setTextSize(14);//设置字体的大小
                    dialog.setIndicatorBackgroundColor(android.R.color.holo_orange_light);//设置指示器的颜色
                    dialog.setTextSelectedColor(android.R.color.holo_orange_light);//设置字体获得焦点的颜色
                    dialog.setTextUnSelectedColor(android.R.color.holo_blue_light);//设置字体没有获得焦点的颜色
                    //            dialog.setDisplaySelectorArea("31",1,"2704",1,"2711",0,"15582",1);//设置已选中的地区
                    dialog.setSelectorAreaPositionListener(this);
                    dialog.show();
                }
                break;
            case R.id.tv_layout_white_right://保存
                name = et_personal_name.getText().toString().trim();
                phone = et_personal_phone.getText().toString().trim();
                mail = et_personal_mail.getText().toString().trim();
                sex = tv_personal_sex.getText().toString().trim();
                birthday = tv_personal_birthday.getText().toString().trim();
                address = tv_personal_city.getText().toString().trim();
                job_time = tv_personal_join_job_time.getText().toString().trim();
                String sex_select = "";
                String job_time_select = "";
                if (TextUtils.isEmpty(name)) {
                    showErrorTip("请输入姓名");
                    return;
                }
                if (TextUtils.isEmpty(sex)) {
                    showErrorTip("请选择性别");
                    return;
                }
                if ("男".equals(sex)) {
                    sex_select = "1";
                } else if ("女".equals(sex)) {
                    sex_select = "2";
                }
                if (TextUtils.isEmpty(birthday)) {
                    showErrorTip("请选择出生年月");
                    return;
                }
                if (TextUtils.isEmpty(job_time)) {
                    showErrorTip("请选择参加工作时间");
                    return;
                }
                if ("至今".equals(job_time)) {
                    job_time_select = "0";
                } else {
                    job_time_select = job_time;
                }
                if (TextUtils.isEmpty(address)) {
                    showErrorTip("请选择现居城市");
                    return;
                }
                if (!RegexUtils.checkMobile(phone)) {
                    showErrorTip("请检查手机号");
                    return;
                }
                if (!RegexUtils.checkEmail(mail)) {
                    showErrorTip("请检查邮箱号");
                    return;
                }
                L.e("=====personal=====", name);
                L.e("=====personal=====", sex_select);
                L.e("=====personal=====", birthday);
                L.e("=====personal=====", job_time_select);
                L.e("=====personal=====", provinceCode);
                L.e("=====personal=====", cityCode);
                L.e("=====personal=====", countyCode);
                L.e("=====personal=====", phone);
                L.e("=====personal=====", mail);
                L.e("=====personal=====", token);
                memberPst.perfectIn(name, sex_select, birthday, job_time_select, provinceCode, cityCode, countyCode,
                        phone, mail, token);
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_personal;
    }

    @Override
    protected void initialized() {
        token = application.getUserInfo().get("data");
        resumePst = new ResumePst(this);
        memberPst = new MemberPst(this);
    }

    @Override
    protected void requestData() {
        resumePst.userInfo(token);
    }

    @Override
    public void selectorAreaPosition(int provincePosition, int cityPosition, int countyPosition, int streetPosition) {
        this.provincePosition = provincePosition;
        this.cityPosition = cityPosition;
        this.countyPosition = countyPosition;
        this.streetPosition = streetPosition;
    }

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        provinceCode = (province == null ? "" : province.code);
        cityCode = (city == null ? "" : city.code);
        countyCode = (county == null ? "" : county.code);
        streetCode = (street == null ? "" : street.code);
        //        String s = (province == null ? "" : province.name) + (city == null ? "" : city.name) + (county ==
        // null ? "" :
        //                county.name) +
        //                (street == null ? "" : street.name);
        String s = (province == null ? "" : province.name) + "-" + (city == null ? "" : city.name) + "-" + (county ==
                null ? "" :
                county.name);
        tv_personal_city.setText(s);
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public void dialogclose() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("userInfo")) {
            L.e("=====userInfo=====", jsonStr);
            PersonalBean personalBean = GsonUtil.GsonToBean(jsonStr, PersonalBean.class);
            et_personal_name.setText(personalBean.getData().getName());
            et_personal_phone.setText(personalBean.getData().getPhone());
            et_personal_mail.setText(personalBean.getData().getEmail());
            if ("1".equals(personalBean.getData().getSex())) {
                tv_personal_sex.setText("男");
            } else if ("2".equals(personalBean.getData().getSex())) {
                tv_personal_sex.setText("女");
            }
            tv_personal_birthday.setText(personalBean.getData().getBirth());
            tv_personal_join_job_time.setText(personalBean.getData().getWorktime());
            tv_personal_city.setText(personalBean.getData().getDomicile_province() + "-" + personalBean.getData()
                    .getDomicile_city() + "-" + personalBean.getData().getDomicile_area());
            return;
        }
        if (requestUrl.contains("perfectIn")) {
            L.e("=====personal=====", jsonStr);
            return;
        }
    }
}
